#include<iostream>
using namespace std;
 #include <sys/types.h>          /* See NOTES */
 #include <sys/socket.h>
#include<unistd.h>
#include<cstring>
#include<arpa/inet.h>
#define PORT 8888
#define BUFFER_SIZE 64
#include"stdShared.h"
#include<json-c/json.h>
struct Msg
{
     int m_type;
    char m_name[BUFFER_SIZE];
    char m_passwd[BUFFER_SIZE];
    /*搜索歌名*/
    char searchSongName[BUFFER_SIZE];
    /*搜索歌手*/
    char searchSinger[BUFFER_SIZE];
    /*喜欢的歌曲*/
    char loveSongName[BUFFER_SIZE];
    /*歌曲风格*/
    char songStyle[BUFFER_SIZE];

};

void menu()
{
   // system("clear ");
    cout<<"1.注册"<<endl;
    cout<<"2.登录"<<endl;
    cout<<"3.退出"<<endl;
}

void registerFuc(int sockfd)
{
        char username[BUFFER_SIZE];
        cout<<"请输入用户名：";
        scanf("%s",username);
        
        char passwd[BUFFER_SIZE];
        cout<<"请输入密码：";
        scanf("%s",passwd);
        /*创建json对象*/
        json_object *jsonObj=json_object_new_object(); 
        json_object_object_add(jsonObj,"type",json_object_new_int(REGISTER));
        json_object_object_add(jsonObj,"username",json_object_new_string(username));
        json_object_object_add(jsonObj,"passwd",json_object_new_string(passwd));

        /*将json对象转换成字符串*/
        const char *jsonStr=json_object_to_json_string(jsonObj);

        // Msg msg;

        // /*发送信息到服务器*/

         write(sockfd,jsonStr,strlen(jsonStr));

}
void loginFunc(int sockfd)
{
     char username[BUFFER_SIZE];
        cout<<"请输入用户名：";
        scanf("%s",username);
        
        char passwd[BUFFER_SIZE];
        cout<<"请输入密码：";
        scanf("%s",passwd);

        /*创建json对象*/
        json_object *jsonObj=json_object_new_object(); 
        json_object_object_add(jsonObj,"type",json_object_new_int(LOGIN));
        json_object_object_add(jsonObj,"username",json_object_new_string(username));
        json_object_object_add(jsonObj,"passwd",json_object_new_string(passwd));

        /*将json对象转换成字符串*/
        const char *jsonStr=json_object_to_json_string(jsonObj);


    //    Msg user(LOGIN,username,passwd);

        /*发送信息到服务器*/
        int length=strlen(jsonStr);
        write(sockfd,&length,sizeof(int));

         write(sockfd,jsonStr,strlen(jsonStr));

    
}



int main()
{
     /*创建通信文件描述符*/
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd==-1)
    {
        perror("create error:");
        _exit(-1);

    }


    /*链接服务器*/
    sockaddr_in serverAddress;
    /*地址族 IPV4*/
    serverAddress.sin_family=AF_INET;
    /*端口*/
    serverAddress.sin_port=htons(PORT);
    /*服务器Ip地址*/
    const char *serverIp="192.168.111.128";
    //主机字节序转换成网络字节序
    inet_pton(AF_INET,serverIp,&(serverAddress.sin_addr.s_addr));


    /*链接服务器*/
    connect(sockfd,(const sockaddr *)&serverAddress,sizeof(serverAddress));



    /*客户端发送信息给服务器*/
    char data[BUFFER_SIZE]={0};
    while (1)
    {
         /*清除脏数据*/
        memset(data,0,sizeof(data));
        /*菜单*/
        menu();
        int choice=0;
        cout<<"请输入：";
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                registerFuc(sockfd);
                break;
            case 2:
                loginFunc(sockfd);
                break;
            case 3:
                break;

            default:
                exit(0);
                break;
        }




        //读取直到换行符的所有字符，包含空格
        //使用fgets 读取一整行，包含空格
        // fgets(str,sizeof(str),stdin);
        // write(sockfd,data,strlen(data));
    }
    
    
    // /*清除脏数据*/
    // memset(data,0,sizeof(data));
    // /*接受服务器发送回的信息*/
    // read(sockfd,data,sizeof(data));

    // cout<<"服务器发送过来的数据是："<<data<<endl;















    return 0;

}